package blog.sys.service;

import blog.sys.dao.ReleaseContentDao;
import blog.sys.dao.SysFileDao;
import blog.sys.data.release.ReleaseContentInfo;
import blog.sys.entity.ReleaseContent;
import blog.sys.entity.SysFile;
import blog.sys.entity.User;
import blog.web.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pub.dao.GeneralDao;
import pub.dao.jdbc.ListQuery;
import pub.dao.query.Query;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author xuyubang
 * @version 1.0
 */
@Service
@Transactional(readOnly = true)
public class ReleaseContentService {

    /**
     * 获取所有用户发布的内容
     *
     * @return
     */
    public List<ReleaseContentInfo> getListReleaseContentInfo() {
        Query<ReleaseContentInfo> query = ListQuery.of(ReleaseContentInfo.class);
        String sql = "SELECT r.* ,(select GROUP_CONCAT(s.id) from t_sys_file s WHERE r.id=s.rel_id) AS photoIds\n" +
                "FROM t_release_content r where 1 order by id desc";
        query.setSql(sql);
        generalDao.execute(query);
        List<ReleaseContentInfo> releaseContentInfos = query.getResult().getRows();
        for (int i = 0; i < releaseContentInfos.size(); i++) {
            Date dateTime = releaseContentInfos.get(i).getCreateTime();
            Integer userId = releaseContentInfos.get(i).getUserId();
            if (dateTime != null) {
                releaseContentInfos.get(i).setCreateForMatTime(DateUtils.showDate(dateTime, "yyyy-MM-dd HH:mm:ss"));
            }
            if (userId != null) {
                User user = userService.get(userId);
                releaseContentInfos.get(i).setNickname(user.getNickName());
            }
        }
        return releaseContentInfos;
    }


    /**
     * 获取当前用户发布的内容
     *
     * @param userId
     * @return
     */
    public List<ReleaseContentInfo> getListReleaseByUserId(Integer userId) {
        Query<ReleaseContentInfo> query = ListQuery.of(ReleaseContentInfo.class);
        String sql = "SELECT c.*,(select GROUP_CONCAT(s.id) from t_sys_file s WHERE c.id=s.rel_id) AS photoIds FROM t_release_content c WHERE c.user_id = " + userId + " AND c.status = 1 ORDER BY c.create_time DESC;";
        query.setSql(sql);
        generalDao.execute(query);
        List<ReleaseContentInfo> releaseContentInfos = query.getResult().getRows();
        if (releaseContentInfos != null && releaseContentInfos.size() > 0) {
            for (ReleaseContentInfo releaseContentInfo : releaseContentInfos) {
                releaseContentInfo.setNickname(userService.get(releaseContentInfo.getUserId()).getNickName());
                releaseContentInfo.setCreateForMatTime(DateUtils.showDate(releaseContentInfo.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            }
        }
        return releaseContentInfos;
    }


    /**
     * 保存发布的内容
     *
     * @param releaseContent
     * @return
     */
    @Transactional
    public Integer onSaveContent(ReleaseContent releaseContent) {
        releaseContentDao.save(releaseContent);
        return releaseContent.getId();
    }


    /**
     * 保存文件
     *
     * @param sysFile
     */
    @Transactional
    public String saveFile(SysFile sysFile) {
        sysFileDao.save(sysFile);
        int id = sysFile.getId();
        return "http://172.16.220.198:8080/jyan-app-server/file/get.do?id="+id;
    }


    /**
     * 返回文件类型
     * @param fileName
     * @return
     */
    public String returnFileType(String fileName) {
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
        String img[] = {"bmp", "jpg", "jpeg", "png", "tiff", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd",
                "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "wmf"};
        for (int i = 0; i < img.length; i++) {
            if (img[i].equals(fileType)) {
                return "图片";
            }
        }

        // 创建文档类型数组
        String document[] = {"txt", "doc", "docx", "xls", "htm", "html", "jsp", "rtf", "wpd", "pdf", "ppt"};
        for (int i = 0; i < document.length; i++) {
            if (document[i].equals(fileType)) {
                return "文档";
            }
        }
        // 创建视频类型数组
        String video[] = {"mp4", "avi", "mov", "wmv", "asf", "navi", "3gp", "mkv", "f4v", "rmvb", "webm"};
        for (int i = 0; i < video.length; i++) {
            if (video[i].equals(fileType)) {
                return "视频";
            }
        }
        // 创建音乐类型数组
        String music[] = {"mp3", "wma", "wav", "mod", "ra", "cd", "md", "asf", "aac", "vqf", "ape", "mid", "ogg",
                "m4a", "vqf"};
        for (int i = 0; i < music.length; i++) {
            if (music[i].equals(fileType)) {
                return "音乐";
            }
        }


        return "其他";
    }


    @Autowired
    private SysFileDao sysFileDao;

    @Autowired
    private GeneralDao generalDao;
    @Autowired
    private ReleaseContentDao releaseContentDao;
    @Autowired
    private UserService userService;


}
